package com.example.controller;

import com.example.dto.job.Job;
import com.example.dto.job.JobCriteria;
import com.example.exception.BadRequestException;
import com.example.server.JobServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "系统：岗位管理")
@RequestMapping("/api/job")
public class JobController {

    @Autowired
    private JobServer jobServer;
    private static final String ENTITY_NAME = "job";

    @ApiOperation("查询岗位")
    @GetMapping
    public ResponseEntity<Object> queryJob(JobCriteria criteria){
        return new ResponseEntity<>(jobServer.list(criteria),HttpStatus.OK);
    }

    @ApiOperation("新增岗位")
    @PostMapping
    public ResponseEntity<Object> create(@RequestBody Job job) {
        if (job.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        jobServer.create(job);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @ApiOperation("删除岗位")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody List<Long> ids){
        jobServer.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("修改岗位")
    @PutMapping
    public ResponseEntity<Object>  update( @RequestBody Job resources) {
        jobServer.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
